package com.ruoyi.common.utils;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.mock.web.MockMultipartFile;

import java.util.Base64;

public class Base64ToMultipartFile {

    public static MultipartFile base64ToMultipart(String base64, String fileName) throws Exception {
        // 去掉 Base64 前缀（比如 data:image/png;base64,）
        if (base64.contains(",")) {
            base64 = base64.split(",")[1];
        }

        byte[] bytes = Base64.getDecoder().decode(base64);
        return new MockMultipartFile(fileName, fileName, "image/jpeg", bytes);
    }
}
